﻿/*
    Copyright 2008 Travis Scholtens
    
    This file is part of Anticipa-Do List.

    Anticipa-Do List is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Anticipa-Do List is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Anticipa-Do List.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Anticipa_Do_List
{
    /// <summary>
    /// Interaction logic for ConfigureNotifierWindow.xaml
    /// </summary>
    partial class ConfigureNotifierWindow : Window
    {
        public ConfigureNotifierWindow(string title, List<KeyValuePair<string, bool>> keys)
            : this(title, keys, new Dictionary<string, string>()) { }

        public ConfigureNotifierWindow(string title, List<KeyValuePair<string, bool>> keys,
            Dictionary<string, string> initial)
        {
            InitializeComponent();

            Title = title;
            Keys = keys;
            InitialValues = initial;
        }

        /// <summary>
        /// Values set by text boxes.
        /// </summary>
        public List<string> Values = null;

        private List<KeyValuePair<string, bool>> Keys;
        private Dictionary<string, string> InitialValues;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            double h = buttonRow.ActualHeight;
            int i;
            for (i = 0; i < Keys.Count; i++)
            {
                var label = new Label();
                label.Content = string.Format("{0}:", Keys[i].Key);
                label.Margin = new Thickness(10);

                FrameworkElement box = null;
                if (Keys[i].Value)
                {
                    var p = new PasswordBox();
                    p.PasswordChar = '*';
                    if (InitialValues.ContainsKey(Keys[i].Key))
                        p.Password = InitialValues[Keys[i].Key];
                    box = p;
                }
                else
                {
                    var t = new TextBox();
                    if(InitialValues.ContainsKey(Keys[i].Key))
                        t.Text = InitialValues[Keys[i].Key];
                    box = t;
                }
                box.Name = string.Format("box{0}", i);
                box.Margin = new Thickness(10);

                var grid = new Grid();
                grid.ColumnDefinitions.Add(new ColumnDefinition());
                grid.ColumnDefinitions.Add(new ColumnDefinition());
                grid.Children.Add(label);
                grid.Children.Add(box);
                box.SetValue(Grid.ColumnProperty, 1);

                panel.Children.Add(grid);

                Height += h;
                contentRow.Height = new GridLength(contentRow.ActualHeight + h);
            }
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        private void ok_Click(object sender, RoutedEventArgs e)
        {
            Values = new List<string>(Keys.Count);
            for (int i = 0; i < Keys.Count; i++)
            {
                UIElement box = ((Grid)panel.Children[i]).Children[1];
                Values.Add(Keys[i].Value ?
                    ((PasswordBox)box).Password :
                    ((TextBox)box).Text);
            }
            DialogResult = true;
            Close();
        }
    }
}
